package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.constant.CommonContants;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrPenaltyRateTableReq;
import com.jrx.anytxn.param.dto.product.PrPenaltyRateTableRes;
import com.jrx.anytxn.param.entity.PrPenaltyRateTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrPenaltyRateTableMapper;
import com.jrx.anytxn.param.service.product.IPrPenaltyRateTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 罚息利率表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@CacheConfig(cacheNames = "PENALTY_RATE")
@Service
public class PrPenaltyRateTableServiceImpl implements IPrPenaltyRateTableService {
    private static final Logger logger = LoggerFactory.getLogger(PrPenaltyRateTableServiceImpl.class);
    @Resource
    private ExtPrPenaltyRateTableMapper extPrPenaltyRateTableMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrPenaltyRateTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrPenaltyRateTable> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrPenaltyRateTable> penaltyRateTableList = extPrPenaltyRateTableMapper.selectByTenId(tenantId);
        if (penaltyRateTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrPenaltyRateTableRes> res = BeanMapping.copyList(penaltyRateTableList, PrPenaltyRateTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrPenaltyRateTableRes addPenaltyRateTable(PrPenaltyRateTableReq prPenaltyRateTableReq) throws TxnException {
        PrPenaltyRateTable prPenaltyRateTable = extPrPenaltyRateTableMapper.selectByTenIdAndPanaltyId(prPenaltyRateTableReq.getTenantId(), prPenaltyRateTableReq.getPenaltyId());
        if (prPenaltyRateTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrPenaltyRateTable penaltyRateTable = BeanMapping.copy(prPenaltyRateTableReq, PrPenaltyRateTable.class);
        try {
            penaltyRateTable.setId(segmentService.getId(Constant.PR_PENALTY_RATE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        penaltyRateTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrPenaltyRateTableMapper.insertSelective(penaltyRateTable);
        return BeanMapping.copy(penaltyRateTable, PrPenaltyRateTableRes.class);
    }

    @CacheEvict(key = "'penaltyRate_'+#prPenaltyRateTableReq.tenantId+'_'+#prPenaltyRateTableReq.penaltyId")
    @Override
    public PrPenaltyRateTableRes modifyPenaltyRateTable(PrPenaltyRateTableReq prPenaltyRateTableReq) throws TxnException {
        PrPenaltyRateTable prPenaltyRateTable = extPrPenaltyRateTableMapper.selectByTenIdAndPanaltyId(prPenaltyRateTableReq.getTenantId(), prPenaltyRateTableReq.getPenaltyId());
        if (prPenaltyRateTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrPenaltyRateTable penaltyRateTable = BeanMapping.copy(prPenaltyRateTableReq, PrPenaltyRateTable.class);
        // 更新
        extPrPenaltyRateTableMapper.updateByTenIdAndPenaltyId(penaltyRateTable);
        return BeanMapping.copy(penaltyRateTable, PrPenaltyRateTableRes.class);

    }

    @CacheEvict(key = "'penaltyRate_'+#tenantId+'_'+#penaltyId")
    @Override
    public Boolean removeByTenIdAndPenaltyId(String tenantId, String penaltyId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(penaltyId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrPenaltyRateTableMapper.deleteByTenIdAndPenaltyId(tenantId, penaltyId);
        return i > 0;
    }

    @Override
    public PrPenaltyRateTableRes findByTenIdAndPanaltyId(String tenantId, String penaltyId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(penaltyId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrPenaltyRateTable penaltyRateTable = extPrPenaltyRateTableMapper.selectByTenIdAndPanaltyId(tenantId, penaltyId);
        return BeanMapping.copy(penaltyRateTable, PrPenaltyRateTableRes.class);
    }


    @Cacheable(key = "'penaltyRate_'+#tenantId+'_'+#penaltyId")
    @Override
    public PrPenaltyRateTable findByPenaltyId(String penaltyId, String orgId, String tenantId) {
        PrPenaltyRateTable prPenaltyRateTable = extPrPenaltyRateTableMapper.selectByPenaltyId(penaltyId, orgId, tenantId);
        return prPenaltyRateTable;
    }

    /**
     * 获取罚息日利率
     *
     * @param dailyRate
     * @param penaltyRateTable
     * @return
     */
    @Override
    public BigDecimal getPenaltyDailyRate(BigDecimal dailyRate, PrPenaltyRateTable penaltyRateTable) {
        BigDecimal penDailyRate = BigDecimal.ZERO;

        if (penaltyRateTable == null) {
            return penDailyRate;
        }
        String increaseType = penaltyRateTable.getIncreaseType();
        BigDecimal increaseNum = penaltyRateTable.getIncreaseNum();

        //上浮百分比
        if (Constant.PENALTY_RATE_INCREASE_TYPE_PERCENT.equals(increaseType)) {
            BigDecimal percent = increaseNum.add(BigDecimal.ONE);
            penDailyRate = dailyRate.multiply(percent).setScale(CommonContants.BIGDECIMAL_SCALE_7, BigDecimal.ROUND_HALF_UP);
        } else {//上浮固定值
            penDailyRate = dailyRate.add(increaseNum).setScale(CommonContants.BIGDECIMAL_SCALE_7, BigDecimal.ROUND_HALF_UP);
        }
        return penDailyRate;
    }
}
